package mdocejb.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionBeans.Interfaces.IGestContactRemote;
import sessionBeans.Interfaces.IGestEntrepriseRemote;

import entityBeans.Contact;
import entityBeans.Entreprise;

/**
 * Servlet implementation class DeleteContact
 */
public class DeleteContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB(name = "GestContactEntity")
	private IGestContactRemote GCR;

	@EJB(name = "GestEntrepriseEntity")
	private IGestEntrepriseRemote GER;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeleteContact() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String ctype = request.getParameter("ctype");
		Long id = Long.valueOf(request.getParameter("userid")).longValue();
		if (id != null) {
			if (ctype.equalsIgnoreCase("person")) {
				Contact cToDelete = GCR.find(id);
				GCR.delete(cToDelete);
			} else if (ctype.equalsIgnoreCase("company")) {
				Entreprise eToDelete = GER.find(id);
				GER.delete(eToDelete);
			} else {
				response.sendRedirect("index.jsp");
			}
		}
		response.sendRedirect("LoadContacts");
	}

}
